package com.wn.demo01;


import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.signers.JWTSigner;
import cn.hutool.jwt.signers.JWTSignerUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;

import java.security.KeyPair;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yellow Docter
 * @date 2023 -07-24
 * @desc
 *  通过证书 对 jwt 进行 加密操作
 */
public class Demo02 {

    public static void main(String[] args) {

        // 构建token中存储的数据，用map存储
        Map<String,Object> playload = new HashMap<>();
        playload.put("userId",100);

        // 创建密钥对对象
        KeyStoreKeyFactory factory = new KeyStoreKeyFactory(
                new ClassPathResource("jwt.jks"),"123456".toCharArray());
        KeyPair keyPair = factory.getKeyPair("jwt");

        // 创建签名器
        JWTSigner jwtSigner = JWTSignerUtil.createSigner("RSA", keyPair);

        //【生成token】
        String token = JWTUtil.createToken(playload, jwtSigner);

    }
}
